﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using VideoClassLibrary.Model;

namespace VideoClassLibrary.BusinessLogicLayer
{
    public class IPCInfoModule
    {
        
        public IPCInfo_Table GetIpcInfoModelById(int RoomId)
        {
            Database db = VDataBase.CreateDatabase();
            string sql = "SELECT * FROM S_R_IPC_Relation_Table A LEFT JOIN IPCInfo_Table B  ON A.IPCID=B.IPCID WHERE A.RID= {0} ";
            sql = string.Format(sql, RoomId);

            DbCommand dbCommand = db.GetSqlStringCommand(sql);
            DataSet dataSet = db.ExecuteDataSet(dbCommand);
            if(dataSet.Tables[0].Rows.Count>0)
            {
                var query = from s in dataSet.Tables[0].AsEnumerable()
                            select new IPCInfo_Table()
                                       {
                                           
                                           IPCID = s.Field<int>("IPCID"),
                                           IPCIp = s.Field<string>("IPCIp"),
                                           IPCName = s.Field<string>("IPCName"),
                                           IPCPort = s.Field<int>("IPCPort"),
                                           IPCPwd = s.Field<string>("IPCPwd"),
                                           IPCStatus = s.Field<int>("IPCStatus"),
                                           IPCUser = s.Field<string>("IPCUser")

                                       };
                return query.FirstOrDefault();
            }


            return null;
        }
        public int UpdateInfoTable(IPCInfo_Table infoTable)
        {
            Database db = VDataBase.CreateDatabase();
            string sql = "UPDATE IPCInfo_Table SET IPCName='{0}',IPCIp='{1}',IPCPort={2},IPCUser='{3}',IPCPwd='{4}' where IPCID={5}";
            sql = string.Format(sql, infoTable.IPCName, infoTable.IPCIp, infoTable.IPCPort, infoTable.IPCUser,
                                infoTable.IPCPwd, infoTable.IPCID);
            DbCommand dbCommand = db.GetSqlStringCommand(sql);
            return db.ExecuteNonQuery(dbCommand);

            //string sql = "SELECT * FROM S_R_IPC_Relation_Table A LEFT JOIN IPCInfo_Table B  ON A.IPCID=B.IPCID WHERE A.RID= {0} ";
            //sql = string.Format(sql, RoomId);
            //DataSet dataSet = db.ExecuteDataSet(sql);
            //if (dataSet.Tables[0].Rows.Count > 0)
            //{
            //    var query = from s in dataSet.Tables[0].AsEnumerable()
            //                select new IPCInfo_Table()
            //                {
            //                    SVRId = s.Field<int>("SVRId"),
            //                    IPCID = s.Field<int>("IPCID"),
            //                    IPCIp = s.Field<string>("IPCIp"),
            //                    IPCName = s.Field<string>("IPCName"),
            //                    IPCPort = s.Field<int>("IPCPort"),
            //                    IPCPwd = s.Field<string>("IPCPwd"),
            //                    IPCStatus = s.Field<int>("IPCStatus"),
            //                    IPCUser = s.Field<string>("IPCUser")

            //                };
            //    return query.FirstOrDefault();
            //}


            return 0;
        }
    }
}
